import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class StudentManager {

    Scanner sc = new Scanner(System.in);

    ArrayList<StudentClass> studentClasses = new ArrayList<>();
    Random rd = new Random();




    public Object[] randomChooseStudent(int classId) {
        StudentClass studentClass = studentClasses.get(classId - 1);
        int studentId = rd.nextInt(studentClass.getStudents().size());
        Student student = studentClass.getStudents().get(studentId);
        return new Object[]{student.getName(), student.getId()};

    }






    public void addStudent(String name, int id, int classId) {
        StudentClass studentClass = studentClasses.get(classId - 1);
        studentClass.getStudents().add(new Student(name, id));
    }

    public Object[][] listStudents(Integer classId) {

        StudentClass studentClass = studentClasses.get(classId - 1);
        Object[][] students = new Object[studentClass.getStudents().size()][2];
        for (int i = 0; i < studentClass.getStudents().size(); i++) {
            students[i][1] = studentClass.getStudents().get(i).getId();
            students[i][0] = studentClass.getStudents().get(i).getName();
        }
        return students;

    }




    public Object[][] listGroups(int classId) {

        StudentClass studentClass = studentClasses.get(classId - 1);
        Object[][] groups = new Object[studentClass.getGroups().size()][5];
        for (int i = 0; i < studentClass.getGroups().size(); i++) {
            groups[i][0] = "小组" + (i + 1);
            groups[i][1] = studentClass.getGroups().get(i).getStu1().getName();
            groups[i][2] = studentClass.getGroups().get(i).getStu1().getId();
            groups[i][3] = studentClass.getGroups().get(i).getStu2().getName();
            groups[i][4] = studentClass.getGroups().get(i).getStu2().getId();
        }
        return groups;

    }

    public void addNewGroup(String name1, int id1, String name2, int id2, int classId) {


        Student student1 = new Student(name1, id1);
        Student student2 = new Student(name2, id2);


        StudentClass studentClass = studentClasses.get(classId - 1);
        studentClass.getStudents().add(student1);
        studentClass.getStudents().add(student2);
        studentClass.getGroups().add(new Group(student1, student2));
    }


    //班级管理


    public void addNewClass() {
        studentClasses.add(new StudentClass());
    }

    public Object[][] listClasses() {
        Object[][] classes = new Object[studentClasses.size()][2];
        for (int i = 0; i < studentClasses.size(); i++) {
            classes[i][0] = "班级" + (i + 1);
            classes[i][1] = studentClasses.get(i).getStudents().size();
        }
        return classes;
    }

    public String[] getAllClasses() {
        String[] classes = new String[studentClasses.size()];
        for (int i = 0; i < studentClasses.size(); i++) {
            classes[i] = "班级" + (i + 1);
        }
        return classes;
    }


    public void recordGrade(int className, String name, int id, double grade) {
        StudentClass studentClass = studentClasses.get(className - 1);
        for (int i = 0; i < studentClass.getStudents().size(); i++) {
            if (studentClass.getStudents().get(i).getId() == id&&studentClass.getStudents().get(i).getName().equals(name)) {
                studentClass.getStudents().get(i).setMarks(grade);
                break;
            }
        }
    }

    public Object[][] getGradesList(int classNum) {
        StudentClass studentClass = studentClasses.get(classNum - 1);
        Object[][] grades = new Object[studentClass.getStudents().size()][3];
        for (int i = 0; i < studentClass.getStudents().size(); i++) {
            grades[i][0] = studentClass.getStudents().get(i).getId();
            grades[i][1] = studentClass.getStudents().get(i).getName();
            grades[i][2] = studentClass.getStudents().get(i).getMarks();
        }
        return grades;
    }
}